package com.android.fragmentdemo.thread18

import kotlinx.coroutines.delay
import org.junit.Test
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadFactory
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit

class Demo18 {

    @Test
    fun testRunnable() {

        val threadPoolExecutor = ThreadPoolExecutor(
            10,
            20,
            10,
            TimeUnit.SECONDS,
            LinkedBlockingQueue(),
            SimpleThreadFactory(),
            ThreadPoolExecutor.DiscardOldestPolicy()
        )
        threadPoolExecutor.execute {
            Thread.sleep(1000)
            println("Use ThreadPoolExecutor")
        }

        //threadPoolExecutor.shutdown()
        //threadPoolExecutor.shutdownNow()

        Thread.sleep(5 * 1000)
    }

    class SimpleThreadFactory : ThreadFactory {
        override fun newThread(r: Runnable?): Thread {
            return Thread(r)
        }
    }
}